package com.app.controller.authority;

import com.app.controller.BaseController;
import com.app.entity.Pagination;
import com.app.entity.authority.Role;
import org.springframework.web.multipart.MultipartFile;
import com.app.entity.authority.Staff;
import com.app.service.authority.StaffService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.List;


@RestController
@RequestMapping("/api/staff")
public class StaffController extends BaseController{
    private static final Logger log = LoggerFactory.getLogger(StaffController.class);


    @Autowired
    private StaffService StaffService;



    @RequestMapping(value="/getInfoAll",method= RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryStaff(@RequestBody Pagination page){
        Map<String, Object> result=new HashMap<String, Object>();
        try {
            Pagination p=this.StaffService.QueryStaffAll(page);
            result.put("code",200);
            result.put("message", "succ");
            result.put("data", p);

            return result;
        } catch (Exception e) {
            log.error("func:queryStaff %parntId:"+page+"%n"+e.getMessage());
            result.put("code", 500);
            result.put("message", e);
            return result;
        }

    }

    @RequestMapping(value="/getInfoDepartID",method= RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getInfoDepartID(@RequestBody Pagination page)
    {
        Map<String, Object> result=new HashMap<String, Object>();
        try {
            Pagination p=this.StaffService.getInfoDepartID(page);
            result.put("code",200);
            result.put("message", "succ");
            result.put("data", p);

            return result;
        } catch (Exception e) {
            log.error("func:queryStaff %parntId:"+page+"%n"+e.getMessage());
            result.put("code", 500);
            result.put("message", e);
            return result;
        }
    }

    @RequestMapping(value="/DeleteStaffInfo",method= RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> DeleteStaff(@RequestBody Staff _staff){

        Map<String, Object> results=new HashMap<String, Object>();
        try {
            Integer resultcount=this.StaffService.DeleteStaffInfo(_staff);

            if (resultcount>0) {
                results.put("code",200);
                results.put("message", "删除成功！");
                results.put("data",_staff);
                return results;
            }
            else {
                results.put("code",201);
                results.put("message", "删除失败");
                results.put("data","-1");
                return results;
            }

        } catch (Exception e) {
            log.error("func:DeleteStaff %n_staff:"+_staff+"%n"+e.getMessage());
            results.put("code", 500);
            results.put("message", e.getMessage());
            return results;
        }
    }
    @PostMapping("/uploadPeopleImg")
    @ResponseBody
    public  Map<String,Object>  UploadImg(@RequestParam("file") MultipartFile file)
    {
        Map<String, Object> result = new HashMap<String,Object>();
        String message;
        try {
            if(file==null){

                message= "图片不存在";
            }
            String fileName = file.getOriginalFilename();

            long size=file.getSize();
            if( size==0){
                message= "图片大小为null";
            }
            boolean r = StaffService.saveImg(fileName,file);
            if(r) {
                message = "上传成功";
                result.put("pic",r);
                result.put("code", 200);
                result.put("MSG", message);
            }
            else
            {
                message="上传失败";
                result.put("pic",r);
                result.put("code", 201);
                result.put("MSG", message);
            }

        }catch (Exception e) {
            result.put("code", 500);
            log.error(String.format("uploadPeopleImg:%s:%s", e.toString(),
                    e.getMessage()));

            System.out.println(String.format("uploadPeopleImg:%s:%s", e.toString(),
                    e.getMessage()));
        }
        return result;
    }


    @RequestMapping(value = "/addStaffInfo", method = RequestMethod.POST)
    @ResponseBody
        public Map<String, Object> insertRole(@RequestBody Staff _staff) {

        //添加ID
        _staff.setId(java.util.UUID.randomUUID().toString());
        Map<String, Object> results = new HashMap<String, Object>();
        try {
            Integer resultcount = this.StaffService.insertStaff(_staff);

            if (resultcount > 0) {
                results.put("code", 200);
                results.put("message", "操作成功！");
                results.put("data", _staff);
            } else {
                results.put("code", 201);
                results.put("message", "角色编码重复，请选择其他角色编号！");
                results.put("data", "-1");
            }
            return results;

        } catch (Exception e) {
            log.error("func:insertStaff %n _staff:" + _staff + "%n" + e.getMessage());
            results.put("code", 500);
            results.put("message", e.getMessage());
            return results;
        }
    }
    @RequestMapping(value = "/getModifyInfo", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getModifyInfo(@RequestBody Staff _staff) {

        Map<String, Object> result = new HashMap<String, Object>();
        try {
            List<Staff> p = this.StaffService.getModifyInfo(_staff);
            result.put("code", 200);
            result.put("message", "succ");
            result.put("data", p);
            return result;

        } catch (Exception e) {
            log.error("func:getModifyInfo %n Staff:" + _staff + "%n" + e.getMessage());
            result.put("code", 500);
            result.put("message", e);
            return result;
        }
    }

    @RequestMapping(value="/updateStaffInfo",method= RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> UpdateStaffInfo(@RequestBody Staff _staff){
        Map<String, Object> result=new HashMap<String, Object>();
        try {
            int p =this.StaffService.UpdateStaffInfo(_staff);
            if(p > 0) {
                result.put("code", 200);
                result.put("message", "修改成功");
                result.put("data", p);
            }else {
                result.put("code", 201);
                result.put("message", "修改失败");
                result.put("data", p);
            }
            return result;
        } catch (Exception e) {
            log.error("func:UpdateStaffInfo %_staff:"+_staff+"%n"+e.getMessage());
            result.put("code", 500);
            result.put("message", e);
            return result;
        }
    }

    @RequestMapping(value="/getInfoByParentId",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryDepartmentByParentId(@RequestBody Staff _department){
        Map<String, Object> result=new HashMap<String, Object>();

/*        try {
            List<Department> depts=this.departmentService.queryDepartByParentId(_department.getParentId());
            if (depts!=null) {
                result.put("code",200);
                result.put("message", "succ");
                result.put("data", depts);

            }
            else {
                result.put("code", 201);
                result.put("message", "notexists");
            }

            return result;
        } catch (Exception e) {
            log.error("func:queryDepartmentByParentId %_department:"+_department+"%n"+e.getMessage());
            result.put("code", 500);
            result.put("message", e);
            return result;
        }*/

       return result;

    }

    @RequestMapping(value = "/getFormInfo", method = RequestMethod.POST)//value属性：映射的是请求的地址;method属性：定义了请求的方式
    @ResponseBody//将controller的方法返回的对象转换为指定的格式之后，写入到response对象的body区，通常用来返回JSON数据或者是XML
    public Map<String, Object> getFormInfo(@RequestBody Staff _staff)
    {
        Map<String, Object> results = new HashMap<>();
        try {

            List<Role> list=this.StaffService.getFormInfo(_staff);

            System.out.println("pageStting在这里-----"+list);
            if (_staff != null) {
                results.put("code", 200);
                results.put("message", "操作成功！");
                results.put("data", list);
            } else {
                results.put("code", 201);
                results.put("message", "操作失败！");
                results.put("data", null);
            }
        } catch (Exception e) {
            results.put("code", 500);
            results.put("message", e);
        }
        return results;
    }

    @RequestMapping(value = "/StaffNumberInfo", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> StaffNumberInfo(@RequestBody Staff _staff)
    {
        Map<String, Object> results = new HashMap<String, Object>();
        try {
            List<Staff> list = this.StaffService.StaffNumberInfo(_staff);;
            if (list.size() > 0) {
                results.put("code", 201);
                results.put("message", "编码存在！请选择其他编码");
            } else {
                results.put("code", 200);
                results.put("message", "succ");
            }
            return results;
        } catch (Exception e) {
            log.error("func:StaffNumberInfo %n _staff:" + _staff + "%n" + e.getMessage());
            results.put("code", 500);
            results.put("message", e.getMessage());
            return results;
        }
    }
}
